#!/usr/bin/python
import time
import sys
import os

def dump_test_list(tests):
	global bad_map
	global good_map
	global mismatch_map
	print "<TABLE><TR><TD>Test</TD><TD>Status</TD><TD></TD></TR>"
	for t in tests:
		print "<TR><TD>" + t + "</TD>"
		if t in bad_map:
			print "<TD style=\"background-color: #ff0000; \">BAD</TD>"
		elif t in good_map:
			print "<TD style=\"background-color: #00ff00; \">OK</TD>"
		else:
			print "<TD style=\"background-color: purple\">???</TD>"
		if t in mismatch_map:
			print "<td style=\"background-color: orange\">MISMATCH</td>"
		else:
			print "<td></td>"
		print "</TR>"
	print "</TABLE>"

if "TRACEPATH" in os.environ:
	testpath=os.environ["TRACEPATH"]
else:
	testpath="tests/traces-out"

if "REPORTNAME" in os.environ:
	reportname=os.environ["REPORTNAME"]
else:
	reportname="TEST DUMP"


bad_tests = open(testpath + "/tests.bad", "r")
ok_tests = open(testpath + "/tests.ok", "r")
mismatch_tests = open(testpath + '/tests.mismatch', 'r')

bad_map = dict()
good_map = dict()
mismatch_map = dict()
all_tests = []
for f in bad_tests:
	bad_map[f[:-1]] = True
	all_tests.append(f[:-1])

for f in ok_tests:
	good_map[f[:-1]] = True
	all_tests.append(f[:-1])

for f in mismatch_tests:
	mismatch_map[f[:-1]] = True

print "<HTML><HEAD><TITLE>GENERATED BY MKREPORT.PY</TITLE></HEAD><BODY>"


print "<h1>" + reportname + "</h1>Date: <i>" + time.ctime() + "</i><br/>Host: <i>" + os.uname()[1] +"</i><hr/>"

all_tests.sort()

time.strftime("%y-%m-%d")


unit_tests = filter(lambda x : "tests" in x, all_tests)
bin_tests = filter(lambda x : not "tests" in x, all_tests)

print "<p>Unit Tests:<br/>"
dump_test_list(unit_tests)
print "</p>"

print "<p>Bin Tests:<br/>"
dump_test_list(bin_tests)
print "</p>"

print "<p>Final Count: <br/>"
print "OK: <b>" + str(len(good_map)) + "</b><br/>"
print "Failed: <b>" + str(len(bad_map)) + "</b><br/>"
print "Mismatched: <b>" + str(len(mismatch_map)) + "</b><br/>"
print "</p>"

print "</BODY></HTML>"
